package main

import (
    "github.com/garyburd/redigo/redis"
    "fmt"
    "strconv"
    "time"
    nsema "github.com/toolkits/concurrent/semaphore"
)


var Pool redis.Pool

func init()  {      //init 用于初始化一些参数，先于main执行
    Pool = redis.Pool{
        MaxIdle:     160,
        MaxActive:   500,
        IdleTimeout: 120,
        Wait :true,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", "127.0.0.1:6379")
        },
    }
}



func main()  {
    sema := nsema.NewSemaphore(500)
    for i := 1; i<=2000; i++ {
        //fmt.Println("a"+ strconv.Itoa(i))
        sema.Acquire()
        go func(key string){
            conn :=Pool.Get()
            _,err := conn.Do("HSET","student",key, key, 0)
            if err != nil {
                fmt.Println("redis set error:", err)
            }   
        }("a"+ strconv.Itoa(i))
    }


    time.Sleep(10*time.Second)
}
